let app = getApp()
import itemID from "@/utils/baseUrl.js"
import itemBase from "@/utils/base64_encode.js"
export function request(config) {
	uni.showLoading()
	let port = itemID.baseUrl;
	// 解构赋值
	let {
		url = '',
			data = {},
			method = 'POST'
	} = {
		...config
	}

	return new Promise((resolve, reject) => {
		uni.request({
			url: port + url,
			header: {
				'Content-Type': 'application/json',
				"Auth-Token": uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
			},
			// withCredentials: true,
			method: method,
			data: {
				...data
			},
			success: (res) => {
				uni.hideLoading()
				if (res.data.result == 1) {
					resolve(res.data)
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
					setTimeout(function(){
						if (res.data?.msg == '请登录' && res.data?.result == 0) {
							// uni.reLaunch({
							// 	url: '/pages/login/index'
							// })
							let currentURL = window.location.href;
							  let yz_redirect = window.location.href.indexOf('/userinfo?') < 0 ? itemBase.base64_encode(currentURL+'?i=41'): '';//不处理userinfo? 更新资料页面，防止死循环一直在这两个页面跳转
							
							window.location = `https://jr.qichen168.com/addons/yun_shop/?menu=#/AuthorizedLogin?i=41&mid=4&type=1&yz_redirect=${yz_redirect}`
						}
							
					},300)
					reject(res.data)
				}
			},
			fail: (err) => {
				uni.hideLoading()
				reject(err)
			}
		});
	})
}